/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "usart.h"
#include "spi.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "app.h"
#include "crc16.h"
#include <stdio.h>
#include <stddef.h>
#include "modbus.h"
#include "filter.h"
//#include "math.h"
#include "common_table.h"
#include "oled.h"
#include "menu.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define EXT_INT_MAX_NBR 22u
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define PRIM_NUM        64U          /* Size of the prime array */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

uint16_t u16InternalTimerCnt = 0;
uint8_t RX_BUFFER[RXBUFFERSIZE]={0};
uint8_t RX_SIZE = 0;
static __attribute__(( aligned(32))) uint32_t primes_ram[PRIM_NUM];
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void __attribute__((section(".ccmsram"))) Prime_Calc_SRAM(void);
void  Prime_Calc_SRAM(void)
{
	/* Compute prime number in the elements size of the array */
	/* Initial condition */
	primes_ram[0] = 1;
	for (uint32_t i = 1; i < PRIM_NUM;)
	{
		for (uint32_t j = primes_ram[i - 1] + 1;; j++)
		{
			for (uint32_t k = 2; k <= j; k++)
			{
				primes_ram[i] = j;
				goto nexti;
			}
		}
		nexti:
		i++;
	}
}


/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

void TIM2_IRQ(void)
{

	if(LL_TIM_IsActiveFlag_UPDATE(TIM2) == SET)//tim2计数器重置回调函�???
	{
		u16InternalTimerCnt++;
		if(u16InternalTimerCnt>=cT17ms)//LED闪烁
		{
			LED_TOGG();
			OLED_Update();
			u16InternalTimerCnt=0;
		}


		LL_TIM_ClearFlag_UPDATE(TIM2);

	}
}



void TIM1_IRQ(void)
{

	static int16_t i = 0;
	static int16_t _100HZ = 0;
	static int16_t _100HZsin = 0;
	static int16_t busv = 0;
	static int16_t busFilterOut = 0;
	static int16_t busFilterOut1 = 0;



	if(LL_TIM_IsActiveFlag_UPDATE(TIM1) == SET)//tim1计数器重置回调函�???
	{
		i++;
		_100HZ= ABS(SinTab50Hz[i]);

		//		if(i<200)
		//		{
		//			_100HZsin = SinTab50Hz[++i]>>2;
		//		}
		//		else
		//		{
		//			_100HZsin = SinTab50Hz[++i];
		//		}
		_100HZsin = SinTab50Hz[++i];
		busv = (400u + ((_100HZsin*20)>>14))*10;
		bus_filter_out.s16Input = busv;
		busFilterOut = Control_Driver_SecondOrderIirfp(&bus_filter_out,14);//陷波器
		bus_filter_out1.s16Input = busv;
		busFilterOut1 = Control_Driver_FirstOrderApf(&bus_filter_out1,14);//全通滤波
		int16_t temp = busv+busFilterOut1;
		u1_printf("%d,%d,%d,%d\r\n",busFilterOut1,busFilterOut,busv,temp);

		if(i>383)
		{
			i = 0;
		}
		LL_TIM_ClearFlag_UPDATE(TIM1);

	}
}


void LPUART_Recive_Callback()
{


	if(LL_LPUART_IsActiveFlag_ORE(LPUART1))//溢出错误中断
	{
		LL_LPUART_ReceiveData8(LPUART1);//读一次RDR寄存�???

		LL_LPUART_ClearFlag_ORE(LPUART1);//清除溢出中断
	}



	if(LL_LPUART_IsActiveFlag_RXNE_RXFNE(LPUART1) && LL_LPUART_IsEnabledIT_RXNE_RXFNE(LPUART1))
	{
		RX_BUFFER[RX_SIZE] = LL_LPUART_ReceiveData8(LPUART1);//输入数据缓冲�???

		RX_SIZE++;
	}
	/* 判断空闲中断标志�??? */
	if (LL_LPUART_IsActiveFlag_IDLE(LPUART1) && LL_LPUART_IsEnabledIT_IDLE(LPUART1))
	{
		// 清除空闲中断标志�???
		LL_LPUART_ClearFlag_IDLE(LPUART1);

		//      u1_printf("\r\n");
		//      RX_SIZE = 0;
		From_Flag = 1;
	}

}



/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_LPUART1_UART_Init();
  MX_TIM2_Init();
  MX_TIM1_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */

//  HAL_SPI_DMAResume(&hspi1);
  OLED_Init();
	Filter_Init();

	LL_TIM_EnableIT_UPDATE(TIM2);    // ENABLE TIM2 IT Update Interrupt <=> TIM_DIER_UIE = 1
	LL_TIM_EnableCounter(TIM2);      // ENABLE TIM2 Counter             <=> TIM_CR1_CEN  = 1
	LL_TIM_EnableAllOutputs(TIM2);

	LL_TIM_EnableIT_UPDATE(TIM1);
	LL_TIM_EnableCounter(TIM1);
	LL_TIM_EnableAllOutputs(TIM1);
	LL_TIM_CC_EnableChannel(TIM1,LL_TIM_CHANNEL_CH1);

	LL_LPUART_EnableIT_IDLE(LPUART1);
	//	LL_LPUART_EnableDMAReq_TX(LPUART1);
	TIM1->ARR = 16000;

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

	while (1)
	{
		menuc1();

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

	}
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  LL_FLASH_SetLatency(LL_FLASH_LATENCY_4);
  while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4)
  {
  }
  LL_PWR_EnableRange1BoostMode();
  LL_RCC_HSE_Enable();
   /* Wait till HSE is ready */
  while(LL_RCC_HSE_IsReady() != 1)
  {
  }

  LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_6, 85, LL_RCC_PLLR_DIV_2);
  LL_RCC_PLL_EnableDomain_SYS();
  LL_RCC_PLL_Enable();
   /* Wait till PLL is ready */
  while(LL_RCC_PLL_IsReady() != 1)
  {
  }

  LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2);
   /* Wait till System clock is ready */
  while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  {
  }

  /* Insure 1us transition state at intermediate medium speed clock*/
  for (__IO uint32_t i = (170 >> 1); i !=0; i--);

  /* Set AHB prescaler*/
  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
  LL_SetSystemCoreClock(170000000);

   /* Update the time base */
  if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
	/* User can add his own implementation to report the HAL error return state */
	__disable_irq();
	while (1)
	{
	}
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
	/* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
